<?php

namespace interface\v1;

// 订货系统 个人中心接口类
use verify;

class myStoreClass extends index
{
	private const  table        = 'store_client';
	private const  tableAddress = 'store_client_address';
	private const  tableTarget  = 'kehu';
	
	private static string $cid  = 'ab3c4d8d235c26ff';
	private static string $khid = 'aff27e1a45c3ab73';
	
	private static function getOrderTotal($state)
	{
		return self::total([
			'table' => 'store_order',
			'where' => [
				'coid'  => self::$coid,
				'khid'  => self::$khid,
				'state' => $state,
			],
		]);
	}
	
	/**
	 * 首页
	 *
	 * @return void
	 */
	public function home()
	{
		$this->code       = 1;
		$info             = query(self::table, " id = '" . self::$cid . "' ");
		$kehu             = query(self::tableTarget, " khid = '{$info['khid']}' ");
		$data['userInfo'] = [
			'avatarUrl'   => root . 'img/miniProgram/store/header.svg',
			'nickName'    => $info['name'],
			'phoneNumber' => $info['tel'],
			'gender'      => $info['gender'],
			'khid'        => $info['khid'],
		];
		// 订单统计
		$data['orderTagInfos'] = [
			[
				'orderNum' => self::getOrderTotal('待下单'), 'tabType' => 5, 'type' => '待下单',
			],
			[
				'orderNum' => self::getOrderTotal('待确认'), 'tabType' => 10, 'type' => '待确认',
			],
			[
				'orderNum' => self::getOrderTotal('待发货'), 'tabType' => 40, 'type' => '待发货',
			],
			[
				'orderNum' => self::getOrderTotal('已完成'), 'tabType' => 0, 'type' => '已完成',
			],
		];
		// 统计信息
		$data['countsData'] = [
			[
				'num' => 2, 'name' => '积分', 'type' => 'point',
			],
			[
				'num' => 10, 'name' => '优惠券', 'type' => 'coupon',
			],
		];
		// 客服信息
		$data['customerServiceInfo'] = [
			'servicePhone'        => para('hotline'),
			'serviceTimeDuration' => '每周一至周五 9:00-12:00  13:00-15:00',
		];
		// 企业信息
		$data['company']    = [
			'info'    => [
				'companyName' => $kehu['companyName'],
				'nature'      => $kehu['nature'],
				'contactName' => $kehu['contactName'],
				'contactTel'  => $kehu['contactTel'],
				'website'     => $kehu['website'],
				'email'       => $kehu['email'],
				'address'     => $kehu['region'] . $kehu['address'],
			],
			// 开票信息
			'invoice' => [
				'companyBank'    => $kehu['companyBank'],
				'companyNum'     => $kehu['companyNum'],
				'companyBankNum' => $kehu['companyBankNum'],
				'companyAddress' => $kehu['companyAddress'],
			],
		];
		$this->resp['data'] = $data;
	}
	
	/**
	 * 收件地址列表
	 *
	 * @return void
	 */
	public function getAddress()
	{
		$this->code = 1;
		$khid       = 'aff27e1a45c3ab73';
		$id         = $this->post('id') ?: '';
		if ($id) {
			$array = query(self::tableAddress, " id = '$id' ", ' `id`, `name`, `tel` as `phone`, `regionId`, `region`, `address` as `detailAddress`, `tag` as `addressTag`, `isDefault`');
			if ($array['id']) {
				$region                = query('region', " id = '{$array['regionId']}' ");
				$array['addressId']    = $array['id'];
				$array['provinceName'] = $region['province'];
				$array['cityName']     = $region['city'];
				$array['districtName'] = $region['area'];
			}
		} else {
			$result = find([
				'table' => self::tableAddress,
				'field' => '`id`, `name`, `tel` as `phoneNumber`, `regionId`, `region`, `address`, `tag`, `isDefault`',
				'where' => " khid = '$khid' ",
			]);
			$array  = $result['array'];
		}
		$this->resp['data'] = $array;
	}
	
	/**
	 * 新增地址
	 *
	 * @return void
	 */
	function putAddress()
	{
		$id    = $this->post('addressId') ?: self::getId();
		$field = [
			'name'      => $this->post('name'),
			'tel'       => $this->post('phone'),
			'regionId'  => $this->post('districtCode') ?: $this->post('regionId'),
			'region'    => $this->post('provinceName') . $this->post('cityName') . $this->post('districtName'),
			'address'   => $this->post('detailAddress'),
			'tag'       => $this->post('addressTag'),
			'isDefault' => $this->post('isDefault') ? 1 : 0,
		];
		// 注释
		$annotation = [
			'name'     => '姓名',
			'tel'      => '手机号',
			'regionId' => '区域',
			'region'   => '区域',
			'address'  => '详细地址',
			'tag'      => '标签',
		];
		// 规则
		$rules = [
			'name'     => 'required|min_len:2|max_len:20',
			'tel'      => 'required|tel',
			'regionId' => 'required|min_len:6|max_len:6',
			'region'   => 'required|min_len:3|max_len:255',
			'address'  => 'required|min_len:3|max_len:255',
		];
		// 校验是否符合规则
		$vt     = new verify();
		$result = $vt->setAliases($annotation)->validate($field, $rules)->isOk();
		if ($result === false) {
			$this->resp['warn'] = $vt->getErrs()[0]; // 多个规则提示最前面一条
			goto resp;
		} else {
			$where = " id = '$id' ";
			$info  = query(self::tableAddress, $where);
			if ($info['id']) {
				$warn = static::update([
					'table' => self::tableAddress,
					'field' => $field,
					'where' => $where,
				]);
			} else {
				$warn = static::insert([
					'table' => self::tableAddress,
					'field' => array_merge($field, [
						'id'   => $id,
						'coid' => self::$coid,
						'khid' => self::$khid,
					]),
				]);
			}
			if (in_array($warn, ['更新成功', '新增成功'])) {
				$this->code         = 1;
				$this->resp['data'] = $id;
				$this->resp['warn'] = '设置成功';
			} else {
				$this->resp['warn'] = '设置失败';
			}
		}
		
		resp:
	}
	
	/**
	 * 删除收件地址
	 *
	 * @return void
	 */
	public function delAddress()
	{
		$id = $this->post('id') ?: '';
		if (!$id) {
			$this->resp['warn'] = '参数错误';
		} else {
			$warn               = delete([
				'table' => self::tableAddress,
				'where' => " id = '$id' AND khid = '" . self::$khid . "' ",
			]);
			$this->resp['warn'] = $warn;
			$this->code         = ($warn === '删除成功') ? 1 : 0;
		}
	}
	
	
	/**
	 * 关于
	 */
	public function about()
	{
		$this->code = 1;
		$type       = $this->post('type');
		switch ($type) {
			case '用户服务及隐私协议':
				$content = query('document', "title = '用户服务及隐私协议' and state = '已发布' order by time desc");
				break;
			default:
				$content = query('document', "title = '公司介绍' and state = '已发布' order by time desc");
				break;
		}
		
		$data['result']     = [
			'text' => html_entity_decode($content['content'], ENT_QUOTES),
		];
		$this->resp['data'] = $data;
	}
	
	/**
	 * 退出登录
	 *
	 * @return void
	 */
	public function loginOut()
	{
		$token = $this->header['Auth'];
		//更新时间
		if ($token) {
			$this->rSet($token, null);
			$this->code         = 1;
			$this->resp['warn'] = '退出成功';
		}
	}
	
}